package com.realjinge.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.realjinge.dao.AnswerDao;
import com.realjinge.dao.QuestionDao;
import com.realjinge.entity.Answer;
import com.realjinge.entity.Question;

/**
 * 
 * @author realjinge
 * 
 */
@Service
public class QuestionService {
	@Resource
	private QuestionDao questionDao;
	@Resource
	private AnswerDao answerDao;

	public Integer insertQuestion(Question question) {
		return questionDao.insertQuestion(question);
	}

	public Integer updateQuestionAnswerBYID(String answer, Integer id) {
		return questionDao.updateQuestionAnswerBYID(answer, id);
	}

	public Integer updateQuestionAnswerBYQuestionNo(String answer, String questionNo) {
		return questionDao.updateQuestionAnswerBYQuestionNo(answer, questionNo);
	}

	public Integer updateQuestionStatusBYID(String status, Integer id) {
		return questionDao.updateQuestionStatusBYID(status, id);
	}

	public Integer updateQuestionStatusBYQuestionNo(String status, String questionNo) {
		return questionDao.updateQuestionStatusBYQuestionNo(status, questionNo);
	}

	public Question selectQuestionByID(Integer id) {
		return questionDao.selectQuestionByID(id);
	}

	public Question selectQuestionByQuestionNo(String questionNo) {
		Question question = questionDao.selectQuestionByQuestionNo(questionNo);
		if (question != null) {
			List<Answer> answerList = answerDao.selectAnswerByQuestionNo(question.getQuestionNo());
			question.setAnswerList(answerList);
		}
		return question;
	}

	public Question selectQuestionByTypeCode(String typeCode) {
		Question question = questionDao.selectQuestionByTypeCode(typeCode);
		if (question != null) {
			List<Answer> answerList = answerDao.selectAnswerByQuestionNo(question.getQuestionNo());
			question.setAnswerList(answerList);
		}
		return question;
	}

	public List<Question> selectQuestionListByCreator(String creator) {
		List<Question> list = questionDao.selectQuestionListByCreator(creator);
		for (int i = 0; i < list.size(); i++) {
			// List<Answer> answerList =
			// answerDao.selectAnswerByQuestionID(list.get(i).getId());
			List<Answer> answerList = answerDao.selectAnswerByQuestionNo(list.get(i).getQuestionNo());
			list.get(i).setAnswerList(answerList);
		}
		return list;
	}

	public List<Question> selectQuestionList() {
		List<Question> list = questionDao.selectQuestionList();
		for (int i = 0; i < list.size(); i++) {
			// List<Answer> answerList =
			// answerDao.selectAnswerByQuestionID(list.get(i).getId());
			List<Answer> answerList = answerDao.selectAnswerByQuestionNo(list.get(i).getQuestionNo());
			list.get(i).setAnswerList(answerList);
		}
		return list;
	}

	public List<Question> selectQuestionPage(Integer startNum, Integer endNum) {
		List<Question> list = questionDao.selectQuestionPage(startNum, endNum);
		for (int i = 0; i < list.size(); i++) {
			// List<Answer> answerList =
			// answerDao.selectAnswerByQuestionID(list.get(i).getId());
			List<Answer> answerList = answerDao.selectAnswerByQuestionNo(list.get(i).getQuestionNo());
			list.get(i).setAnswerList(answerList);
		}
		return list;
	}

}
